import {ref, watch} from "vue";
import {useSocket} from "@/socket.js";
import {useRoute} from "vue-router";


export function useRouteParams() {
    const route = useRoute()
    const chatId = ref('')
    const groupId = ref('')

    watch(route,async (newRoute) => {
        useSocket().emit("leave chat", chatId.value)
        if (newRoute.name === 'channel') {
            groupId.value = newRoute.params.groupId?.trim()
            chatId.value = newRoute.params.channelId?.trim()
        } else if (newRoute.name === 'conversation') {
            groupId.value = ''
            chatId.value = newRoute.params.dmId?.trim()
        } else {
            chatId.value = ''
            groupId.value = ''
        }

        console.log(chatId.value)

        useSocket().emit("join chat", chatId.value);
    }, {immediate: true})

    return {chatId, groupId}
}
